﻿using System;
using KLS.PPDS.Core.Entity;
using KLS.PPDS.Core.Entity.BaseEntities;
using KLS.PPDS.Data.Database.Repository.EntityRepository;
using KLS.PPDS.Resources.Logging;
using KLS.PPDS.Utilities.Enums;
using KLS.PPDS.Utility;

namespace KLS.PPDS.ApplicationService.Manager
{
    public class LogManager: RepositoryManager<LogRepository>
    {
        
        /// <summary>
        /// Log write to db
        /// </summary>
        /// <param name="t"></param>
        /// <param name="feature"></param>
        /// <param name="actionName"></param>
        /// <param name="description"></param>
        public static void Log(Entity t, Feature feature, ActionName actionName, string description)
        {
            var log = new Log();
                log.ObjectId = t.Id;
                log.Feature = feature.ToString();
                log.Object = t.GetType().Name;
                log.Action = actionName.ToString();
                log.Description = description;
                log.Date = DateTime.Now;
                log.UserId = CommonSession.UserId;
                log.UserName = CommonSession.UserName;
                log.Company = CommonSession.CompanyName;
            Save(log);            
        }

        /// <summary>
        /// log assuming feature is the type of entity
        /// </summary>
        /// <param name="t"></param>
        /// <param name="actionName"></param>
        /// <param name="description"></param>
        public static void Log(Entity t,  ActionName actionName, string description)
        {
            var log = new Log();
                log.ObjectId = t.Id;
                log.Feature = t.GetType().Name;
                log.Object = t.GetType().Name;
                log.Action = actionName.ToString();
                log.Description = description;
                log.Date = DateTime.Now;
                log.UserId = CommonSession.UserId;
                log.UserName = CommonSession.UserName;
                log.Company = CommonSession.CompanyName;

            Save(log);            
        }
        

        private static void Save(Log  log)
        {
            Repository.SaveAndEvict(log);
        }


        public static void LogDbAdd(Entity t)
        {            
            Log(t, ActionName.Add, DatabaseLoggingString.DatabaseAdd);
        }

        public static void LogDbAdd(Entity t, Feature feature)
        {
            Log(t, feature, ActionName.Add, DatabaseLoggingString.DatabaseAdd);
        }


        public  static void LogDbUpdate(Entity t)
        {
            Log(t, ActionName.Update, DatabaseLoggingString.DatabaseUpdate);
        }

        public static void LogDbUpdate(Entity t, Feature feature)
        {
            Log(t, feature, ActionName.Update, DatabaseLoggingString.DatabaseUpdate);
        }

        public static void LogDbDelete(Entity t)
        {
            Log(t, ActionName.Delete, DatabaseLoggingString.DatabaseDelete);
        }

        public static void LogDbDelete(Entity t, Feature feature)
        {
            Log(t, feature, ActionName.Delete, DatabaseLoggingString.DatabaseDelete);
        }
        
    }
}
